package cn.edu.neusoft.zw725.foodorder.model;

import cn.edu.neusoft.zw725.foodorder.bean.RegBean;
import cn.edu.neusoft.zw725.foodorder.iface.RegIface;
import cn.edu.neusoft.zw725.foodorder.listener.RegListener;
import cn.edu.neusoft.zw725.foodorder.service.RegService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by china on 2017/11/1.
 */

public class RegModel implements RegIface {
    private RegService regService;
    private Retrofit retrofit;
    private static final String BASE_URL = "http://60.205.189.39/";
    public RegModel(){
        retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getRegBean(String username, String userpass, String mobilenum, String address, String comment, final RegListener regListener) {
        regService = retrofit.create(RegService.class);
        Call<RegBean> call = regService.getRegBean(username,userpass,mobilenum,address,comment);
        call.enqueue(new Callback<RegBean>() {
            @Override
            public void onResponse(Call<RegBean> call, Response<RegBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    regListener.onResponse(response.body());
                }
                else {
                    regListener.onFail("error");
                }
            }

            @Override
            public void onFailure(Call<RegBean> call, Throwable t) {
                regListener.onFail("error");
            }
        });
    }
}
